package com.asset.dispoal.verdict.service.common;

import com.asset.dispoal.verdict.domain.db.entity.GbJudgmentBookItem;
import com.asset.dispoal.verdict.domain.db.entity.GbJudgmentBookItemDetail;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookItemDetailMapper;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookItemMapper;
import com.asset.dispoal.verdict.dto.request.CreateOrEditItemRequest;
import com.asset.dispoal.verdict.service.converter.GbJudgmentBookItemConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-07
 */
@Repository
public class EditOrCreateItemCommand implements BaseCommand<CreateOrEditItemRequest, Result> {
    @Autowired
    private BaseIdeable<String> snowFlake;
    @Autowired
    private GbJudgmentBookItemMapper gbJudgmentBookItemMapper;
    @Autowired
    private GbJudgmentBookItemDetailMapper gbJudgmentBookItemDetailMapper;

    @Override
    @Transactional
    public Result execute(CreateOrEditItemRequest request) {
        //项目表对象
        GbJudgmentBookItem judgmentBookItem = GbJudgmentBookItemConverter.INSTANCE.reqToDoItem(request);

        if (StringUtil.isBlank(judgmentBookItem.getId())) {
            //新增
            String itemId = snowFlake.generateId();
            judgmentBookItem.setId(itemId);
            if (!CollectionUtils.isEmpty(judgmentBookItem.getJudgmentBookItemDetailList())) {
                for (GbJudgmentBookItemDetail itemDetail : judgmentBookItem.getJudgmentBookItemDetailList()) {
                    itemDetail.setId(snowFlake.generateId());
                    itemDetail.setItemId(itemId);
                }
                gbJudgmentBookItemDetailMapper.insertBatch(judgmentBookItem.getJudgmentBookItemDetailList());
            }
            int insertRes = gbJudgmentBookItemMapper.insert(judgmentBookItem);
            return insertRes <= 0 ? Result.faild("插入数据失败") : Result.ok();
        } else {
            //修改
            judgmentBookItem.setBookId(null);
            int updateRes = gbJudgmentBookItemMapper.update(judgmentBookItem);
            //删除项目相关详情
            gbJudgmentBookItemDetailMapper.delDetailByItemId(judgmentBookItem.getId());
            //新增项目相关详情
            int detailRes = -1;
            if (!CollectionUtils.isEmpty(judgmentBookItem.getJudgmentBookItemDetailList())) {
                for (GbJudgmentBookItemDetail itemDetail : judgmentBookItem.getJudgmentBookItemDetailList()) {
                    itemDetail.setId(snowFlake.generateId());
                    itemDetail.setItemId(judgmentBookItem.getId());
                }
                detailRes = gbJudgmentBookItemDetailMapper.insertBatch(judgmentBookItem.getJudgmentBookItemDetailList());
            }
            return (updateRes <= 0 && detailRes <= 0) ? Result.faild("更新数据失败") : Result.ok();
        }
    }
}
